/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/pgl.h>
#include <pgl/core/PGLTextureBrush.h>
#include <pgl/core/PGLRegion.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

PGL_IMPLEMENT_DYNCREATE(CPGLTextureBrush, CPGLBrush);

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CString CPGLTextureBrush::WrapModeToString(EWrapMode eWrapMode)
{
	CString str;

	switch(eWrapMode)
	{
	case WrapModeTileFlipX:
		str=_T("flipx");
		break;
	case WrapModeTileFlipY:
		str=_T("flipy");
		break;
	case WrapModeTileFlipXY:
		str=_T("flipxy");
		break;
	case WrapModeClamp:
		str=_T("clamp");
		break;
	default:
		str=_T("tile");
		break;
	}

	return str;
}

CPGLTextureBrush::EWrapMode CPGLTextureBrush::StringToWrapMode(LPCTSTR szWrapMode)
{
	CString str = szWrapMode;
	str.MakeLower();

	if (str==_T("flipx"))
		return WrapModeTileFlipX;
	else if (str==_T("flipy"))
		return WrapModeTileFlipY;
	else if (str==_T("flipxy"))
		return WrapModeTileFlipXY;
	else if (str==_T("clamp"))
		return WrapModeClamp;
	else 
		return WrapModeTile;
}

void CPGLTextureBrush::SerializeXML(CPGLArchive& ar)
{
	static TCHAR szTag[] = _T("CPGLTextureBrush");
	static CString str;

	markup::CMarkupArchive& xml = *((markup::CMarkupArchive*)ar.GetMarkup());
	if(ar.IsStoring())
	{
		xml.AddChildElem(szTag);
		xml.IntoElem();
			CPGLBrush::SerializeXML(ar);

			m_image.SerializeXML(ar);
			xml.AddChildElem(_T("WrapMode"), WrapModeToString(m_eWrapMode));

		xml.OutOfElem();
	}
	else
	{
		if  (!xml.FindChildElem(szTag))
				return;

		xml.IntoElem();
			CPGLBrush::SerializeXML(ar);

			m_image.SerializeXML(ar);
			if(xml.FindGetChildData(_T("WrapMode"), str))
				m_eWrapMode = StringToWrapMode(str);	

		xml.OutOfElem();
	}
}

#ifdef _DEBUG
void CPGLTextureBrush::Dump( CDumpContext& dc ) const
{
    // call base class function first
    CPGLBrush::Dump( dc );

    // now do the stuff for our specific class
	dc << _T("CPGLTextureBrush \n");
}
void CPGLTextureBrush::AssertValid() const
{
    // call inherited AssertValid first
    CPGLBrush::AssertValid();

} 
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPGLTextureBrush::CPGLTextureBrush(LPCTSTR szFileName, const CPGLRectD& rBBox)
: CPGLBrush(rBBox), m_eWrapMode(WrapModeTile)
{
	m_image.Load(szFileName);
}

CPGLTextureBrush::~CPGLTextureBrush()
{
}

CPGLTextureBrush& CPGLTextureBrush::operator = (const CPGLTextureBrush& brush)
{
	if (this != &brush)
	{
		this->CPGLBrush::operator=(brush);
	
		m_image = brush.m_image;
		m_eWrapMode = brush.m_eWrapMode;
	}
	return *this;
}

void CPGLTextureBrush::MakeCurrentFill(gfxinterface::CGfxInterface& gfx)
{
	using namespace gfxinterface;
	
	// no bitmap
	if (m_image.GetSourceType() == CPGLImage::SourceTypeNone)
		return;

	SRectD rect;
	rect.left=0;
	rect.top=0;
	rect.width=m_image.GetWidth();
	rect.height=m_image.GetHeight();

	gfx.SetTextureFill(m_image.GetGDIpBitmap(), rect, GetGfxWrapMode(m_eWrapMode));
}

gfxinterface::ETextureWrapMode CPGLTextureBrush::GetGfxWrapMode(EWrapMode eWrapMode)
{
	using namespace gfxinterface;

	switch(eWrapMode)
	{
	case WrapModeTileFlipX:
		return TexWrapModeTileFlipX;
	case WrapModeTileFlipY:
		return TexWrapModeTileFlipX;
	case WrapModeTileFlipXY:
		return TexWrapModeTileFlipXY;
	case WrapModeClamp:
		return TexWrapModeClamp;
	default:
		return TexWrapModeTile;
	}
}
